import { Say } from '@libs/db/models/say.model';
import { Body, Controller, Get, Param, Post, Put, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiTags } from '@nestjs/swagger';
import { ReturnModelType } from '@typegoose/typegoose';
import { Crud } from 'nestjs-mongoose-crud';
import { InjectModel } from 'nestjs-typegoose';
import { CurrentUser } from '../auth/current-user.decorator';


@Crud({
    model: Say,
    routes: {
        create: false,
        update: false
     }
})
@Controller('says')
@ApiTags('说说')
export class SaysController {
    constructor(
        @InjectModel(Say) private model: ReturnModelType<typeof Say>
    ){}

    @Get("user:id")
    findOneU(@Param("id") id: string) {
        return this.model
        .findById(id)
        .populate('user')
        .populate('wallpaper')
    }


    @Post('user')
    @UseGuards(AuthGuard('jwt'))
    async createU(@Body() dto, @CurrentUser() user){
        dto.user = user._id
        return await this.model.create(dto)
    }

    @Put("user:id")
    @UseGuards(AuthGuard('jwt'))
    async updateU(@Param("id") id: string, @Body() dto, @CurrentUser() user){
        dto.user = user._id
        await this.model.findOneAndUpdate({ _id: id }, dto, {
            new: true,
            upsert: false,
            runValidators: true
          });
        return await this.model.findById(id).populate('user').populate('wallpaper')
    }

}
